# -*- coding: utf-8 -*-
from collections import defaultdict

from kivy.app import App
from hat_widget import *
from kivy.uix.label import Label
from model import gamestate


def len_of_guessed_words(WGR_list):
    return sum(1 if i.result == 0 else 0 for i in WGR_list)


def creating_result_table(rounds_list):
    """
    Create 3 dict.
    1)results_for_person_guessed : player name -> guessed words count
    2)result_for_person_described : player_name -> described_words count
    3)result_for_pair: (player name, player name) -> pair's score
    Args:
        rounds_list: list of gamestate.Round
    """
    result_for_pair = defaultdict(lambda: 0)
    result_for_person_guessed = defaultdict(lambda: 0)
    result_for_person_described = defaultdict(lambda: 0)

    for r in rounds_list:
        pair_names = (r.players_pair[0].name, r.players_pair[1].name)
        p1, p2 = pair_names
        guessed = len_of_guessed_words(r.words)
        result_for_person_guessed[p1] += guessed
        result_for_person_described[p2] += guessed
        result_for_pair[frozenset(pair_names)] += guessed

    return (result_for_pair, result_for_person_guessed,
            result_for_person_described)


class NameLabel(Label):
    pass


class PlayerResultLabel(Label):
    pass


class PairResultLabel(Label):
    pass


class BorderLabel(Label):
    pass


class CurResults(HatWidget):
    def __init__(self, gamestate, presenter, **kwargs):
        super(CurResults, self).__init__(**kwargs)
        self.results_panel.init_widget(gamestate, presenter)
        self.presenter = presenter
        self.gamestate = gamestate

    def end_game(self):
        self.presenter.restart()

    def back(self):
        self.presenter.change_phase(gamestate.Phase.FROM_TO)

    def sort_style(x, sortid):
        return

    def go_back(self):
        self.back()


def create_round(p1, p2, cnt):
    from model.gamestate import *

    game_round = GameRound(0, [], None, (p1, p2))
    for i in range(cnt):
        word_guess_result = WordGuessResult(Word(p1, u"W"),
                                            WordGuessResult.GUESS, 5)
        game_round.words.append(word_guess_result)
    return game_round


class CurResultsApp(App):
    def build(self):
        from model.gamestate import *

        players = Players([Player(u'Андрей', []), Player(u'Петр', []),
                           Player(u'Иван', []), Player(u'Ольга', []),
                           Player(u'Миша', []), Player(u'Петя', [])])
        players.set_next_pair_strategy(OddStrategy(players.players))
        hat = Hat([u'стул', u'шапка', u'кастрюлька'])
        rounds = [
            create_round(players.players[0], players.players[1], 10),
            create_round(players.players[2], players.players[3], 15),
            create_round(players.players[1], players.players[0], 15),
            create_round(players.players[3], players.players[2], 5),
            create_round(players.players[4], players.players[5], 5),
        ]

        gamestate = GameState(players, hat, None, 0,
                              rounds, None, Settings(20, 30))
        sample = CurResults(gamestate, None)
        return sample


if __name__ == '__main__':
    from kivy.lang import Builder
    import os.path

    Builder.load_file(os.path.join(os.path.dirname(__file__),
                                   'resultsgeneral.kv'))
    CurResultsApp().run()
